const express=require('express')
const router=express.Router()
const jiami=require('../module/jiami')
const sqlQuery = require('../module/lcMysql')

router.get('/',(req,res)=>{
    res.render('login/login.ejs')
})
router.post('/',async (req,res)=>{
    let username=req.body.username
    let pass=req.body.password
    let sqlStr="select id from user where username = ? and password = ?"
    let result=await sqlQuery(sqlStr,[username,jiami(pass)])
    if(result.length>0){
        req.session.username=username
        console.log(req.session.username,1);
        
        res.json({msg:"ok"})
    }else{
        res.json({msg:"用户名或密码错误"})
    }
    
})

router.get('/register',(req,res)=>{
    res.render('login/register.ejs')
})

router.post('/register',async (req,res)=>{
    let username=req.body.username
    let pass=req.body.password
    let sqlStr="select id from user where username = ?"
    let result=await sqlQuery(sqlStr,[username])
    if(result.length>0){
        res.json({msg:"该用户名已经注册"})
    }else{
        sqlStr="insert into user (username,password) values(?,?)"
        await sqlQuery(sqlStr,[username,jiami(pass)])
        res.json({msg:"ok"})
    }
})


module.exports=router;